/*
Author : Nguyen Van Ty
Email  : ngvanty03@gmail.com
Date   : 1/24/2011 

*/

using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using BusinessObject;
using Utility;
using System.Xml;
namespace DataAccessLayer
{
    /// <summary>
    /// Define CurencyDaoBase class. This class user for DataAccessLayer, contain 4 function Insert, Update, Search, Delete.
    /// If you want add new function, you must define in CurencyDao class , not here.
    /// </summary>
    public class CurencyDaoBase
    {
        private static ILog logger = null;
        #region Constructor
        /// <summary>
        /// Define Constructor.
        /// </summary>
        public CurencyDaoBase() { }
        static CurencyDaoBase()
        {
            //Init logger.
            logger = LoggingUtility.GetLogger(typeof(CurencyDaoBase).FullName);
        }
        #endregion
        /// <summary>
        /// Read data from IDataReader, convert to Curency Object.
        /// </summary>
        public static Curency GetCurencyByDataReader(IDataReader reader)
        {
            String methodName = "GetCurencyByDataReader";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                Curency result = new Curency();
                //read Code field data.

                result.Code = ManagerDao.GetstringDataFromDataReader(reader, "Code");

                //read Name field data.

                result.Name = ManagerDao.GetstringDataFromDataReader(reader, "Name");

                //read Order field data.

                result.Order = ManagerDao.GetintDataFromDataReader(reader, "Order");
                result.IsPublished = ManagerDao.GetboolDataFromDataReader(reader, "IsPublished");
                //read CreatedBy field data.

                result.CreatedBy = ManagerDao.GetstringDataFromDataReader(reader, "CreatedBy");

                //read CreatedDate field data.

                result.CreatedDate = ManagerDao.GetdatetimeDataFromDataReader(reader, "CreatedDate");

                //read LastUpdatedBy field data.

                result.LastUpdatedBy = ManagerDao.GetstringDataFromDataReader(reader, "LastUpdatedBy");

                //read LastUpdatedDate field data.

                result.LastUpdatedDate = ManagerDao.GetdatetimeDataFromDataReader(reader, "LastUpdatedDate");

                return result;
            }
            catch (Exception e)
            {
                // write error to log file.
                logger.Error(methodName, e);
                return null;
            }
        }
        /// <summary>
        /// Define function Insert for Curency Object.
        /// </summary>
        /// <param name="obj">Object Insert.</param>
        /// <returns>
        ///    1: Successfull.
        ///    0: Process Error.
        ///    -1: Item is exists
        ///</returns>
        public static int Insert(Curency obj)
        {
            
            String methodName = "InsertCurency";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            DbCommand cmd = ManagerDao.GetStoredProcCommand("spInsertCurency");

            //Add value "Code"  paramater.
            ManagerDao.AddInParameter(cmd, "Code", DbType.AnsiString, obj.Code);
            //Add value "Name"  paramater.
            ManagerDao.AddInParameter(cmd, "Name", DbType.String,
                ConvertValueObject.CastObjValueToInsertDB(obj.Name));

            ManagerDao.AddInParameter(cmd, "IsPublished", DbType.Boolean,
               ConvertValueObject.CastObjValueToInsertDB(obj.IsPublished));

            //Add value "Order"  paramater.
          //  ManagerDao.AddInParameter(cmd, "Order", DbType.Int32,
            //    ConvertValueObject.CastObjValueToInsertDB(obj.Order));
            //Add value "CreatedBy"  paramater.
            ManagerDao.AddInParameter(cmd, "CreatedBy", DbType.String,
                ConvertValueObject.CastObjValueToInsertDB(obj.CreatedBy));
            //Add value "CreatedDate"  paramater.
            ManagerDao.AddInParameter(cmd, "CreatedDate", DbType.DateTime,
                ConvertValueObject.CastObjValueToInsertDB(obj.CreatedDate));
            //Add value "LastUpdatedBy"  paramater.
            ManagerDao.AddInParameter(cmd, "LastUpdatedBy", DbType.String,
                ConvertValueObject.CastObjValueToInsertDB(obj.LastUpdatedBy));
            //Add value "LastUpdatedDate"  paramater.
            ManagerDao.AddInParameter(cmd, "LastUpdatedDate", DbType.DateTime,
                ConvertValueObject.CastObjValueToInsertDB(obj.LastUpdatedDate));
            //add paramater store return value.        
            ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
            int result = 0;
            try
            {
                ManagerDao.ExecuteNonQuery(cmd);
                result = (int)ManagerDao.GetParameterValue(cmd, "returnValue");
            
            }
            catch (Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
               
            }
            return result;
        }

        /// <summary>
        /// Define function Update for Curency Object.
        /// </summary>
        /// <param name="obj">Object Update.</param>
        /// <returns>
        ///    1: Successfull.
        ///    0: Process Error.
        ///    -1: Item is exist
        ///</returns>
        public static bool Update(Curency obj)
        {
            bool result = false;
            String methodName = "UpdateCurency";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            DbCommand cmd = ManagerDao.GetStoredProcCommand("spUpdateCurency");


            //Add value "Code"  paramater.
            ManagerDao.AddInParameter(cmd, "Code", DbType.AnsiString, ConvertValueObject.CastObjValueToInsertDB(obj.Code));
            //Add value "Name"  paramater.
            ManagerDao.AddInParameter(cmd, "Name", DbType.String, ConvertValueObject.CastObjValueToInsertDB(obj.Name));
            ManagerDao.AddInParameter(cmd, "IsPublished", DbType.Boolean,
              ConvertValueObject.CastObjValueToInsertDB(obj.IsPublished));


            //Add value "Order"  paramater.
            //ManagerDao.AddInParameter(cmd, "Order", DbType.Int32, ConvertValueObject.CastObjValueToInsertDB(obj.Order));
            //Add value "CreatedBy"  paramater.
            //ManagerDao.AddInParameter(cmd, "CreatedBy", DbType.String, ConvertValueObject.CastObjValueToInsertDB(obj.CreatedBy));
            //Add value "CreatedDate"  paramater.
            //ManagerDao.AddInParameter(cmd, "CreatedDate", DbType.DateTime, ConvertValueObject.CastObjValueToInsertDB(obj.CreatedDate));
            //Add value "LastUpdatedBy"  paramater.
            ManagerDao.AddInParameter(cmd, "LastUpdatedBy", DbType.String, ConvertValueObject.CastObjValueToInsertDB(obj.LastUpdatedBy));
            //Add value "LastUpdatedDate"  paramater.
            ManagerDao.AddInParameter(cmd, "LastUpdatedDate", DbType.DateTime, ConvertValueObject.CastObjValueToInsertDB(obj.LastUpdatedDate));
            // add parameter store return value.
            ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
            try
            {
                ManagerDao.ExecuteNonQuery(cmd);
                int rows = (int)ManagerDao.GetParameterValue(cmd, "returnValue");
                if (rows == 1)//Successfull.
                {
                    result = true;
                }
                else//Error.
                {
                    result = false;
                }

            }
            catch (Exception e)
            {
                //write error Message to log file.
                logger.Error(methodName, e);
                result = false;
            }
            return result;
        }
        /// <summary>
        /// Get Data Curency object. 
        /// </summary>
        /// <param name="code" >This is primary key to get data.</param>
        /// <returns>Curency object.</returns>

        public static Curency Get(string code)
        {
            String methodName = "GetCurency";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                Curency result = null;
                DbCommand dbCommnand = ManagerDao.GetStoredProcCommand("spGetCurency");
                //Add value "Code"  paramater.
                ManagerDao.AddInParameter(dbCommnand, "Code", DbType.AnsiString, code);
                IDataReader reader = ManagerDao.ExecuteReader(dbCommnand);
                if (reader.Read())
                {
                    result = GetCurencyByDataReader(reader);

                }
                reader.Close();
                return result;
            }
            catch (System.Exception e)
            {
                //write error message to log file.
                logger.Error("Get", e);
                return null;
            }
        }
        /// <summary>
        /// Get All Curency object from table Curency.
        /// </summary>
        /// <returns>List Curency object.</returns>
        public static List<CurencyResult> GetAll()
        {
            String methodName = "GetAll";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                List<CurencyResult> listCurency = new List<CurencyResult>();
                DbCommand dbCommand = ManagerDao.GetStoredProcCommand("spGetAllCurency");

                using (IDataReader reader = ManagerDao.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        listCurency.Add(GetCurencyResultByDataReader(reader));
                    }
                    reader.Close();
                }

                return listCurency;
            }
            catch (Exception ex)
            {
                //write error message to log file.
                logger.Error(methodName, ex);
                return null;
            }
        }
        public static List<CurencyResult> GetCurencyDTNhapHang()
        {
            String methodName = "GetCurencyDTNhapHang";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                List<CurencyResult> listCurency = new List<CurencyResult>();
                DbCommand dbCommand = ManagerDao.GetStoredProcCommand("spGetCurencyDTNhapHang");

                using (IDataReader reader = ManagerDao.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        listCurency.Add(GetCurencyResultByDataReader(reader));
                    }
                    reader.Close();
                }

                return listCurency;
            }
            catch (Exception ex)
            {
                //write error message to log file.
                logger.Error(methodName, ex);
                return null;
            }
        }
        /// <summary>
        /// Search Curency object in table Curency with condition is t object.
        /// </summary>
        /// <param name="t">Condition for Search.</param>       
        /// <param name="totalRow">  total records in this table.</param>
        /// <returns>List CurencyResult object.</returns>
        public static List<CurencyResult> SearchCurencySomeCondition(Curency t)
        {
            String methodName = "SearchCurencySomeCondition";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                List<CurencyResult> listCurency = new List<CurencyResult>();
                DbCommand cmd = ManagerDao.GetStoredProcCommand("spSearchCurencyByCondition");
                //Add value Condition  paramater.
                ManagerDao.AddInParameter(cmd, "Condition", DbType.String, ConvertValueObject.CastObjValueToInsertDB(t.Condition));

                //Add value PageIndex  paramater.
                ManagerDao.AddInParameter(cmd, "PageIndex", DbType.Int32, t.ObjectPaging.PageIndex);
                //Add value PageSize  paramater.
                ManagerDao.AddInParameter(cmd, "PageSize", DbType.Int32, t.ObjectPaging.PageSize);
                //Add value SortField  paramater.
                ManagerDao.AddInParameter(cmd, "SortField", DbType.String, t.ObjectSortOrder.SortField);
                //Add value SortExpression  paramater.
                ManagerDao.AddInParameter(cmd, "SortExpression", DbType.String, t.ObjectSortOrder.SortExpression);
                using (IDataReader reader = ManagerDao.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        listCurency.Add(GetCurencyResultByDataReader(reader));
                        if (t.ToltalRow <= 0)
                            t.ToltalRow = ManagerDao.Getint32DataFromDataReader(reader, "TotalRow");
                    }

                    reader.Close();
                }

                return listCurency;
            }
            catch (Exception ex)
            {
                //write error message to log file.
                logger.Error(methodName, ex);
                return null;
            }
        }
        /// <summary>
        /// Delete Curency object. 
        /// </summary>
        /// <param name="Code" >This is primary key to delete.</param>
        /// <returns>True/False.</returns>

        public static bool Delete(string Code)
        {
            String methodName = "DeleteCurency";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }

            try
            {
                DbCommand dbCommand = ManagerDao.GetStoredProcCommand("spDeleteCurency");
                //Add value "Code"  paramater.
                ManagerDao.AddInParameter(dbCommand, "Code", DbType.AnsiString, Code);
                //Add value returnValue  paramater.
                ManagerDao.AddParameter(dbCommand, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
                try
                {
                    ManagerDao.ExecuteNonQuery(dbCommand);
                    int parameterValue = (int)ManagerDao.GetParameterValue(dbCommand, "returnValue");
                    if (parameterValue == 1)
                    {
                        return true;
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                return false;
            }
            catch (Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
                return false;
            }
        }
        /// <summary>
        /// Read data from IDataReader, convert to CurencyResult Object.
        /// </summary>
        private static CurencyResult GetCurencyResultByDataReaderByLanguage(IDataReader reader, string LanguageId)
        {
            String methodName = "GetCurencyResultByDataReader";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                CurencyResult result = new CurencyResult();
                //read Code field data.
                result.Code = ManagerDao.GetstringDataFromDataReader(reader, "Code");

                //read Name field data.
                result.Name = ManagerDao.GetstringDataFromDataReader(reader, "Name");

                //read Order field data.
                result.Order = ManagerDao.GetintDataFromDataReader(reader, "Order");

                //read CreatedBy field data.
                result.CreatedBy = ManagerDao.GetstringDataFromDataReader(reader, "CreatedBy");

                //read CreatedDate field data.
                result.CreatedDate = ManagerDao.GetdatetimeDataFromDataReader(reader, "CreatedDate");

                //read LastUpdatedBy field data.
                result.LastUpdatedBy = ManagerDao.GetstringDataFromDataReader(reader, "LastUpdatedBy");

                //read LastUpdatedDate field data.
                result.LastUpdatedDate = ManagerDao.GetdatetimeDataFromDataReader(reader, "LastUpdatedDate");

                return result;
            }
            catch (Exception e)
            {
                // write error to log file.
                logger.Error(methodName, e);
                return null;
            }
        }
        /// <summary>
        /// Read data from IDataReader, convert to CurencyResult Object.
        /// </summary>
        private static CurencyResult GetCurencyResultByDataReader(IDataReader reader)
        {
            String methodName = "GetCurencyResultByDataReader";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            try
            {
                CurencyResult result = new CurencyResult();
                //read Code field data.
                result.Code = ManagerDao.GetstringDataFromDataReader(reader, "Code");

                //read Name field data.
                result.Name = ManagerDao.GetstringDataFromDataReader(reader, "Name");

                //read Order field data.
                result.Order = ManagerDao.GetintDataFromDataReader(reader, "Order");
                result.IsPublished = ManagerDao.GetboolDataFromDataReader(reader, "IsPublished");

                //read CreatedBy field data.
                result.CreatedBy = ManagerDao.GetstringDataFromDataReader(reader, "CreatedBy");

                //read CreatedDate field data.
                result.CreatedDate = ManagerDao.GetdatetimeDataFromDataReader(reader, "CreatedDate");

                //read LastUpdatedBy field data.
                result.LastUpdatedBy = ManagerDao.GetstringDataFromDataReader(reader, "LastUpdatedBy");

                //read LastUpdatedDate field data.
                result.LastUpdatedDate = ManagerDao.GetdatetimeDataFromDataReader(reader, "LastUpdatedDate");

                return result;
            }
            catch (Exception e)
            {
                // write error to log file.
                logger.Error(methodName, e);
                return null;
            }
        }
        public static bool Publish(Curency obj)
        {
            bool result = false;
            String methodName = "Publish";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            DbCommand cmd = ManagerDao.GetStoredProcCommand("spPublishCurency");

            //Add value "Code"  paramater.
            ManagerDao.AddInParameter(cmd, "Code", DbType.String, obj.Code);

            //add paramater store return value.        
            ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
            try
            {
                ManagerDao.ExecuteNonQuery(cmd);
                Int32 id = Int32.Parse(ManagerDao.GetParameterValue(cmd, "returnValue").ToString());

                if (id > 0)//Successfull.
                {
                    result = true;
                }
                else//Error.
                {
                    result = false;
                }
            }
            catch (Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
                result = false;
            }
            return result;
        }

        public static bool Order(Curency obj, int type)
        {
            bool result = false;
            String methodName = "Order";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            DbCommand cmd = ManagerDao.GetStoredProcCommand("spOrderCurency");

            //Add value "Code"  paramater.
            ManagerDao.AddInParameter(cmd, "Code", DbType.String, obj.Code);
            //Add value "SubCategoryId"  paramater.
            ManagerDao.AddInParameter(cmd, "Type", DbType.Int32, type);


            //add paramater store return value.        
            ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
            try
            {
                ManagerDao.ExecuteNonQuery(cmd);
                Int32 id = Int32.Parse(ManagerDao.GetParameterValue(cmd, "returnValue").ToString());

                if (id > 0)//Successfull.
                {
                    result = true;
                }
                else//Error.
                {
                    result = false;
                }
            }
            catch (Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
                result = false;
            }
            return result;
        }
    }
}